package com.cdshangpai.utils.storage;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.cdshangpai.photoapp.LogConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class FileInput extends FileBase {
    FileInputStream mFis;
    InputStream mis;

    public FileInput(Context context) {
        super(context);
    }

    /**
     * 自适应android11的文件读取操作
     *
     * @param context
     * @param folder
     * @param fileName
     */
    public FileInput(Context context, String folder, String fileName) {
        super(context);
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            String pathName = Environment.getExternalStorageDirectory()+File.separator + folder;
            File path = new File(pathName);
            if (!path.exists()) {
                path.mkdirs();
            }
            File file = new File(pathName, fileName);
            try {
                mFis = new FileInputStream(file);
                Log.i(LogConfig.logTag,"加载文件成功"+file.getAbsolutePath());
            } catch (Exception e) {
                Log.i(LogConfig.logTag,"加载文件"+file.getAbsolutePath()+"失败"+e.getMessage());
            }
        } else {
            try {
                mis = openFile(fileName);
            } catch (Exception e) {
            }
        }
    }

    public InputStream getInputStream()
    {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
        return mFis;
        }else{
            return mis;
        }
    }

    /**
     * android11 打开文件获取输入流
     *
     * @param fileName
     * @return
     */
    private InputStream openFile(String fileName) {
        Uri uri = Uri.parse(fileName);
        try {
            InputStream is = mContext.getContentResolver().openInputStream(uri);
            return is;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 获取文件内容长度
     * @return
     */
    public Integer getFileLenght()
    {
        try{
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                return mFis.available();
            }
            else
            {
                return mis.available();
            }
        }catch (Exception e){}
        return 0;
    }

    /**
     * 删除文件
     *
     * @param folder
     * @param fileName
     */
    public void delete(String folder, String fileName) {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            String pathName = Environment.getExternalStorageDirectory()+File.separator + folder;
            File file = new File(pathName, fileName);
            if (file.exists()) {
                file.delete();
                Log.i(LogConfig.logTag,"删除图片成功,文件"+file.getAbsolutePath());
            }else
            {
                Log.i(LogConfig.logTag,"删除文件失败，文件不存在"+file.getAbsolutePath());
            }
        } else {
            try{
                Uri uri = Uri.parse(fileName);
                mContext.getContentResolver().delete(uri, null, null);
            }catch (Exception e){
                Log.i(LogConfig.logTag,"删除图片文件错误,目录："+folder+",文件名:"+fileName);
            }
        }
    }

    public void deleteForFileName(String folder, String fileName) {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            String pathName = Environment.getExternalStorageDirectory()+File.separator + folder;
            File file = new File(pathName, fileName);
            if (file.exists()) {
                file.delete();
                Log.i(LogConfig.logTag,"删除图片成功,文件"+file.getAbsolutePath());
            }else
            {
                Log.i(LogConfig.logTag,"删除文件失败，文件不存在"+file.getAbsolutePath());
            }
        } else {
            try{
                Uri uri = getFileUrl(folder,fileName);
                int r=mContext.getContentResolver().delete(uri, null, null);
                Log.i(LogConfig.logTag,"删除文件"+fileName+"的结果"+r);
            }catch (Exception e){
                Log.i(LogConfig.logTag,"删除图片文件错误,目录："+folder+",文件名:"+fileName);
            }
        }
    }

    public int read(byte[] buf, int offset, int len) {
        try {
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                return mFis.read(buf, offset, len);
            } else {
                return mis.read(buf, offset, len);
            }
        } catch (Exception e) {
        }
        return 0;
    }

    public int read(byte[] buf) {
        try {
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                return mFis.read(buf);
            } else {
                return mis.read(buf);
            }
        } catch (Exception e) {
        }
        return 0;
    }

    public void close() {
        try {
            if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                mFis.close();
            } else {
                mis.close();
            }
        } catch (Exception e) {
        }
    }
}
